2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex3_masterTxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
68 //
69 
70 //******************************************************************************
71 //*****************************************************************************
72 //
73 //Set the address for slave module. This is a 7-bit address sent in the
74 //following format:
75 //[A6:A5:A4:A3:A2:A1:A0:RS]
76 //
77 //A zero in the "RS" position of the first byte means that the master
78 //transmits (sends) data to the selected slave, and a one in this position
79 //means that the master receives data from the slave.
80 //
81 //*****************************************************************************
82 #define SLAVE_ADDRESS 0x48
83 
84 uint8_t TXData =0; // Pointer to TX data
85 uint8_t TXByteCtr;
86 
87 void main(void)
88 {
89  WDT_A_hold(WDT_A_BASE);
90 
91  //Set DCO frequency to 1MHz
92  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_0);
93  //Set ACLK = VLO with frequency divider of 1
94  CS_initClockSignal(CS_ACLK,CS_VLOCLK_SELECT,CS_CLOCK_DIVIDER_1);
95  //Set SMCLK = DCO with frequency divider of 1
96  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
97  //Set MCLK = DCO with frequency divider of 1
98  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
99 
100  // Configure Pins for I2C
101  //Set P1.6 and P1.7 as Secondary Module Function Input.
102  /*
103 
104  * Select Port 1
105  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
106  */
107  GPIO_setAsPeripheralModuleFunctionInputPin(
108  GPIO_PORT_P1,
109  GPIO_PIN6 + GPIO_PIN7,
110  GPIO_SECONDARY_MODULE_FUNCTION
111  );
112 
113  /*
114  * Disable the GPIO power-on default high-impedance mode to activate
115  * previously configured port settings
116  */
117  PMM_unlockLPM5();
118 
119  EUSCI_B_I2C_initMasterParam param = {0};
120  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
121  param.i2cClk = CS_getSMCLK();
122  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
123  param.byteCounterThreshold = 0;
124  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
125  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
126 
127  //Specify slave address
128  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
130  );
131 
132  //Set Master in receive mode
133  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
134  EUSCI_B_I2C_TRANSMIT_MODE
135  );
136 
137  //Enable I2C Module to start operations
138  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
139 
140  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
141  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
142  EUSCI_B_I2C_NAK_INTERRUPT
143  );
144 
145  //Enable master Receive interrupt
146  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
147  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
148  EUSCI_B_I2C_NAK_INTERRUPT
149  );
150  while(1)
151  {
152  __delay_cycles(1000); // Delay between transmissions
153  TXByteCtr = 4; // Load TX byte counter
154  TXData = 0;
155 
156  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent
157  (EUSCI_B0_BASE));
158 
159  EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE,
160  TXData++);
161 
162  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
163  // Remain in LPM0 until all data
164  // is TX'd
165  // Increment data byte
166  }
167 }
168 
169 //------------------------------------------------------------------------------
170 // The USCIAB0TX_ISR is structured such that it can be used to transmit any
171 // number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
172 // points to the next byte to transmit.
173 //------------------------------------------------------------------------------
174 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
175 #pragma vector=USCI_B0_VECTOR
176 __interrupt
177 #elif defined(__GNUC__)
178 __attribute__((interrupt(USCI_B0_VECTOR)))
179 #endif
180 void USCIB0_ISR(void)
181 {
182  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
183  {
184  case USCI_NONE: // No interrupts break;
185  break;
186  case USCI_I2C_UCALIFG: // Arbitration lost
187  break;
188  case USCI_I2C_UCNACKIFG: // NAK received (master only)
189  //resend start if NACK
190  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
191  break;
192  case USCI_I2C_UCTXIFG0: // TXIFG0
193  // Check TX byte counter
194  if (TXByteCtr)
195  {
196  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData++);
197  // Decrement TX byte counter
198  TXByteCtr--;
199  }
200  else
201  {
202  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
203  // Exit LPM0
205  }
206  break;
207  default:
208  break;
209  }
210 }
211 
void USCIB0_ISR(void)
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)